/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import com.edi.runtime.Event.*;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	import mx.controls.TextArea;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	import mx.managers.SystemManager;
	
	import richtexttoolbar.PopupRichTextToolBar;
	
		
	/**
	 * 在MXML中的用法示例：<EDITextArea notNull="false"  fieldLength="500"  visible="true"  primaryKey="false"  fieldType="string"  table="meetingitem"  field="meetingname"  id="meetingNameTxt"  height="22"  x="68" y="34" width="577"></EDITextArea>
	 * 
	 */
	public class EDITextArea extends TextArea   implements IEDIComponent
	{
		private var _field:String;
		private var _fieldtype:String;
		private var _fieldlength:int;
		
		private var _table:String;
		private var _primaryKey:String="";
		
		/****
		 * 是否显示富文本编辑框
		 * true为显示富文本编辑框,false为不显示富文本编辑框
		 * 默认为false,保证向前兼容.
		 */
		public var showRichToolBar:Boolean = false;
		public var notNull:Boolean;
		/**
		 * EDITextArea构造函数
		 * 
		 */		
		public function EDITextArea(showRichToolBar:Boolean = false)
		{
			super();
			//this.addEventListener(FocusEvent.FOCUS_OUT,fouceOutHandler);
			this.showRichToolBar = showRichToolBar;
			this.addEventListener(MouseEvent.MOUSE_UP,mousePressUpHandler);
		}
		/****
		 * 监听鼠标松开事件
		 * 并判断showRichToolBar属性
		 * 来判断是否为TextArea添加富文本编辑框
		 */
		
		private function mousePressUpHandler(e:Event):void{
			if(showRichToolBar){
				this.popupRichTextToolbar(this);
			}
		}
		/**
		 * 实现IEDIComponent接口的属性
		 * @param value
		 * 
		 */		
		public function set table(value:String):void
		{
			_table=value;
			EDIEventProxy.listenerDataRolling(value,this);
		}
		/**
		 * 实现IEDIComponent接口的属性
		 * @return 
		 * 
		 */		
		public function get table():String
		{return _table;}
		/**
		 * 实现IEDIComponent接口的属性
		 * @param value
		 * 
		 */		
		public function set primaryKey(value:String):void
		{_primaryKey=value;}
		/**
		 * 实现IEDIComponent接口的属性
		 * @return 
		 * 
		 */		
		public function get primaryKey():String
		{return _primaryKey;}
		/**
		 * 实现IEDIComponent接口的属性
		 * @param value
		 * 
		 */		
		public function set field(value:String):void
		{_field=value;}
		/**
		 * 实现IEDIComponent接口的属性
		 * @return 
		 * 
		 */		
		public function get field():String
		{return _field;}
		/**
		 * 实现IEDIComponent接口的属性，同时对录入数据的合法性进行检查
		 * @param value
		 * 
		 */		
		public function set fieldType(value:String):void
		{
			_fieldtype=value;
			switch(value)
			{
				case "int":
				{
					restrict="0-9";
					break;
				}
				case "float":
				{
					restrict="0-9\." 
					break;
				}
				case "decimal":
				{
					restrict="0-9\." 
					break;
				}
			}
		}
		/**
		 * 实现IEDIComponent接口的属性，同时对录入数据的合法性进行检查
		 * @return 
		 * 
		 */		
		public function get fieldType():String
		{return _fieldtype;}
		/**
		 * 字段长度，同时对录入数据的合法性进行检查
		 * @param value
		 * 
		 */			
		public function set fieldLength(value:int):void
		{
			_fieldlength=value
			maxChars=value;
		}
		/**
		 * 字段长度，同时对录入数据的合法性进行检查
		 * @return 
		 * 
		 */		
		public function get fieldLength():int
		{return _fieldlength;}
		/**
		 * 实现IEDIComponent接口方法
		 * @param event
		 * 
		 */		
		public function dataRollingHandler(event:EDIDataEvent):void
		{
			text=event.value[field];
		}
		/**
		 * 实现IEDIComponent接口的属性
		 * @return 
		 * 
		 */	
		public function get ediValue():String
		{
			return text;
		}
		
		
		//build by hw 为添加富文本编辑框服务的方法
		private static var _textAreaDic:Dictionary = new Dictionary();
//		private var _defaultTips:String = "点击显示控制面板";
//		
//		public function set defaultTips(value:String):void{
//			this.toolTip = value;
//		}
//		
//		public function get defaultTips():String{
//			return this._defaultTips;
//		}
//		private  function addTarget(textArea:TextArea):void
//		{	
//			textArea.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
//			
//		}
		
		public function removeTarget():void
		{
			this.removeEventListener(MouseEvent.MOUSE_UP,mousePressUpHandler);
		}
		
		private function stageMouseDownHandler(event:MouseEvent):void
		{			
			for each(var toolbar:PopupRichTextToolBar in _textAreaDic)
			{
				if(toolbar)
				{
					if(event.target is SystemManager)
					{
						toolbar.tryRemoveToolbar(event.target,new Point(event.localX,event.localY));
					}
					else
					{
						toolbar.tryRemoveToolbar(event.target);
					}
				}
			}		
		}
		
		private function mouseUpHandler(event:Event):void
		{										
			popupRichTextToolbar(event.currentTarget as TextArea);
		}
		
		
//		override public  function focusOutHandler(event:FocusEvent):void
//		{
//			setTextAreaTips(event.currentTarget as TextArea);	
//		}
		
//		public  function setTextAreaTips():void
//		{
//			if(this.text=="")
//			{
//				this.text = DEFAULT_TIPS;
//			}
//		}
//		
//		private  function clearTextAreaTips(textArea:TextArea):void
//		{
//			if(textArea.text == DEFAULT_TIPS)
//			{
//				textArea.text = "";
//			}	
//		}
		
		
		
		private  function popupRichTextToolbar(textArea:TextArea):void
		{			
			if(_textAreaDic[textArea] == null && textArea.stage)
			{						
				textArea.stage.addEventListener(MouseEvent.MOUSE_DOWN, stageMouseDownHandler);	
				var toolbar:PopupRichTextToolBar = PopUpManager.createPopUp(textArea.parent,PopupRichTextToolBar) as PopupRichTextToolBar;
				toolbar.target = textArea;			
				toolbar.addEventListener(CloseEvent.CLOSE,closeHandler);
				_textAreaDic[textArea] = toolbar;				
			}	
		}

		// remove reference for gc
		private  function closeHandler(event:CloseEvent):void
		{			
			var toolbar:PopupRichTextToolBar = event.currentTarget as PopupRichTextToolBar;
			toolbar.removeEventListener(CloseEvent.CLOSE,closeHandler);						

			if(toolbar.target.stage)
			{
				toolbar.target.stage.removeEventListener(MouseEvent.MOUSE_DOWN,stageMouseDownHandler);
			}
			
			_textAreaDic[toolbar.target] = null;
			PopUpManager.removePopUp(toolbar);										
		}
		//build by hw
		
		//build by hw 2010-11-01
		override public   function set editable(value:Boolean):void{
			super.editable = value;
			this.showRichToolBar = false;
			this.removeEventListener(MouseEvent.MOUSE_UP,mousePressUpHandler);
		}
		
		 override public function set enabled(value:Boolean):void{
		 	super.enabled = value;
		 	this.showRichToolBar = false;
			this.removeEventListener(MouseEvent.MOUSE_UP,mousePressUpHandler);
		 }
		
		////build by hw 2010-11-01
		
	}
}